﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ue.Components.FileResources;

namespace Ue.Tests.Components.FileResources
{
    /// <summary>
    /// ResourceFrameworkTetsts 的摘要说明
    /// </summary>
    [TestClass]
    public class FileResourceManagerTests
    {
        [TestMethod]
        public void GetBinaryResourceString()
        {
            File.Create("Resource1.resources").Dispose();
            ResourceWriter resourceWriter = new ResourceWriter("Resource1.resources");
            resourceWriter.AddResource("ResourceName", "资源名称");
            resourceWriter.Generate();
            resourceWriter.Dispose();

            BinaryResourceManager binaryResourceManager = new BinaryResourceManager("", "Resource1");

            var result = binaryResourceManager.GetString("ResourceName");

            Assert.AreEqual("资源名称", result);
        }

        [TestMethod]
        [DeploymentItem("Components/FileResources/Resource1.resx")]
        public void GetRexResourceString()
        {
            ResxResourceManager resxResourceManage = new ResxResourceManager("","Resource1");

            Assert.AreEqual("资源名称", resxResourceManage.GetString("ResourceName"));
        }

        [TestMethod]
        public void XmlResourceManager()
        {
            XmlResourceWriter writer = new XmlResourceWriter("Resource3.xml");

            FakeResource resource = new FakeResource();
            resource.Address = "中国";
            resource.FirstName = "李";
            resource.LastName = "绍伟";

            writer.AddResource("resource", resource);

            writer.AddResource("name", "资源名称");

            writer.Generate();
            writer.Dispose();

            XmlResourceManager xmlResourceManager = new XmlResourceManager(string.Empty, "Resource3");

            Assert.AreEqual("资源名称", xmlResourceManager.GetString("name"));

            resource = xmlResourceManager.GetObject("resource") as FakeResource;

            Assert.IsNotNull(resource);
            Assert.AreEqual("中国", resource.Address);
            Assert.AreEqual("李", resource.FirstName);
            Assert.AreEqual("绍伟", resource.LastName);
        }

        [TestMethod]
        public void GetResourceByCache()
        {
            File.Create("Resource1.xml").Dispose();
            XmlResourceWriter resourceWriter = new XmlResourceWriter("Resource1.xml");
            resourceWriter.AddResource("ResourceName", "资源名称");
            resourceWriter.Generate();
            resourceWriter.Dispose();

            XmlResourceManager resourceManager = new XmlResourceManager("", "Resource1");

            var result = resourceManager.GetString("ResourceName");

            result = resourceManager.GetString("ResourceName");

            Assert.AreEqual("资源名称", result);
        }

        [TestMethod]
        public void Test()
        {
            XmlDocument document = new XmlDocument();
            document.AppendChild(document.CreateElement("roots"));
            document.FirstChild.AppendChild(document.CreateElement("add"));
 
            document.FirstChild.InnerXml += "<add name=\"home\" path=\"index.aspx\" />";

            document.Save("1.xml");
        }
    }

    public class FakeResource
    {
        [XmlAttribute("firstName")]
        public string FirstName { get; set; }

        [XmlAttribute("lastName")]
        public string LastName { get; set; }

        [XmlElement("address")]
        public string Address { get; set; }
    }
}
